#include "sdk_common.h"
#include "sdk_model_curtain.h"
#include "sdk_proto.h"
#include <stdio.h>
#include <string.h>
#include "sdk.h"




int sdk_model_curtain_ctl(ENUM_CURTAIN_CTL_TYPE type,uint16_t addr, P_CURTAIN_CTL_DATA data)
{
    char json[500]={0};
    char ctldata[100]={0};
    uint16_t attrid= 0;
    if(data== NULL){
        sdk_printf("%s:null pointer\r\n",__FUNCTION__);
        return SDK_NULLPTR_ERR;
    }
    if(addr==0){
        sdk_printf("%s:addr==0\r\n",__FUNCTION__);
        return SDK_PARAMS_ERR;
    }

    if(sdk_proto_context_get()==NULL){
        sdk_printf("%s:sdk context is null\r\n",__FUNCTION__);
        return SDK_UNAUTH_ERR;
    }

    memset(ctldata,0,sizeof(ctldata));
    

    switch(type)
    {
        case enum_curtain_ctl_motor:
            //D1F90917401002
            sprintf(ctldata,"%02x",data->motorstatus);
            attrid = SDK_CURTAIN_CTL_MOTOR_ATTRID;
        break;
        case enum_curtain_ctl_motor_position_abs:
            //D1F9091941101C
            sprintf(ctldata,"%02x",data->motorposition_percentage_abs);
            attrid = SDK_CURTAIN_CTL_MOTOR_POSITIO_ABS_ATTRID;
        break;
        case enum_curtain_set_direction:
            //D1F9091A431001
            sprintf(ctldata,"%02x",data->direction);
            attrid = SDK_CURTAIN_SET_DIRECTION_ATTRID;
        break;
        case enum_curtain_set_handdrag:
            //D1F9091A441001
            sprintf(ctldata,"%02x",data->handdrag_direction);
            attrid = SDK_CURTAIN_SET_HANDDRAG_ATTRID;
        break;
        case enum_curtain_ctl_motor_position_rel:
            //D1F9091A4510011C
            sprintf(ctldata,"%02x%02x",data->motorposition_precentage_rel.incOrdec,data->motorposition_precentage_rel.percentage);
            attrid = SDK_CURTAIN_CTL_MOTOR_POSITION_REL_ATTRID;
        break;
        case enum_curtain_set_dimming_limit:
            //D1F9091A461000
            sprintf(ctldata,"%02x",data->dimming_limit);
            //sprintf(ctldata,"%s","");
            attrid = SDK_CURTAIN_SET_DIMMING_LIMIT_ATTRID;
        break;
        case enum_curtain_ctl_angle_percentage:
            //D1F9091A471010
            sprintf(ctldata,"%02x",data->angel_precentage);
            attrid = SDK_CURTAIN_CTL_ANGLE_PERCENTAGE_ATTRID;
        break;
        case enum_curtain_ctl_angle_direction:
            //D1F9091A471010
            sprintf(ctldata,"%02x",data->angel_direction);
            attrid = SDK_CURTAIN_CTL_ANGLE_DIRECTION_ATTRID;
        break;

        default:
            sdk_printf("%s:unkown option\r\n",__FUNCTION__);
        break;
    }

    sprintf(json,"{\"call\":\"ai.argrace.gw.data.set\",\"ak\":\"%s\",\"gwid\":%u,\"ad\":\"%04x\",\"op\":\"%s\",\"vma\":\"%02x%02x\",\"data\":\"%s\",\"tid\":\"%02x\",\"mac\":\"%s\"}",\
        sdk_proto_context_get()->accesskey,sdk_proto_context_get()->gwid,addr,VMAD1,attrid&0xFF,(attrid>>8)&0xFF,ctldata,sdk_proto_get_tid(),sdk_context.mac);

    sdk_printf("%s:%s\r\n",__FUNCTION__,json);
    return sdk_proto_ctl_send((uint8_t *)json,strlen(json)+1);
}



